//
//  TryHoneLocalNotificationHelper.m
//  TryHone
//
//  Created by TryHone on 17/5/26.
//  Copyright © 2017年 TryHone. All rights reserved.
//

#import "JFNotificationHelper.h"
#import <UIKit/UIKit.h>
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
#import <UserNotifications/UserNotifications.h>
#endif

@implementation JFNotificationHelper
/**
 *  发送本地消息通知
 *
 *  @param fireDate 时间
 *  @param title   标题
 *  @param msg     内容
 */
+(void)sendNotification:(NSDate*)fireDate title:(NSString*)title msg:(NSString*)msg
{
    [JFNotificationHelper sendNotification:fireDate title:title msg:msg viewTitle:@"查看" userInfo:nil];
}

/**
 *  发送本地消息通知
 *
 *  @param fireDate   时间
 *  @param title     标题
 *  @param msg       内容
 *  @param viewTitle 查看标题
 */
+(void)sendNotification:(NSDate*)fireDate title:(NSString*)title msg:(NSString*)msg viewTitle:(NSString*)viewTitle
{
    [JFNotificationHelper sendNotification:fireDate title:title msg:msg viewTitle:viewTitle userInfo:nil];
}

/**
 *  发送本地消息通知
 *
 *  @param fireDate   时间
 *  @param title     标题
 *  @param msg       内容
 *  @param viewTitle 查看标题
 *  @param dataDic   携带的数据
 */
+(void)sendNotification:(NSDate*)fireDate title:(NSString*)title msg:(NSString*)msg viewTitle:(NSString*)viewTitle userInfo:(NSDictionary*)dataDic
{
    
    if (@available(iOS 10.0,*)) {
        // 通知中心
        UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
        // 通知内容
        UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc]init];
        content.title = title;
//        content.subtitle = @"通知子标题";
        content.body = msg;
        // 默认铃声
        content.sound = [UNNotificationSound defaultSound];
        // 角标
        content.badge = @1;
        content.userInfo = dataDic;

        // 设置多长时间之后发送
        if(!fireDate){
            fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
        }
        NSTimeInterval time = [fireDate timeIntervalSinceNow];
        UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:time repeats:NO];

        // id：便于以后移除、更新 指定通知
        NSString * noticeId = @"LocNotice";
        // 通知请求
        UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:noticeId content:content trigger:trigger];
        // 添加通知请求
        [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            if (error == nil) {
                NSLog(@"本地推送成功");
            }
        }];

    }else{
          UILocalNotification *localNotif = [[UILocalNotification alloc] init];
          if (localNotif == nil)
          {
              return;
          }
           if(!fireDate){
                 fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
           }
          localNotif.fireDate = fireDate;
          localNotif.timeZone = [NSTimeZone defaultTimeZone];
          localNotif.alertBody = msg;
          localNotif.alertAction = viewTitle;
          localNotif.soundName = UILocalNotificationDefaultSoundName;
          localNotif.userInfo = dataDic;
          [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
          localNotif = nil;
    }
}

/**
 *  取消所有
 */
+(void)cancelAll
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}

@end
